﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUDataAcquisition.AppCode;
using RTUDataAcquisition.DataAccess;

namespace RTUDataAcquisition
{
    public partial class DataBaseSetting : Form
    {
        public DataBaseSetting()
        {
            InitializeComponent();
        }

        private void DataBaseSetting_Load(object sender, EventArgs e)
        {
            try
            {
                txtIP1.Text = INIConfig.DBSource;
                nudPort1.Value = Convert.ToInt32(INIConfig.DBSourcePort);
                txtDBName1.Text = INIConfig.DBName;
                txtRealData.Text = "RealData";
                txtHis.Text = "";
                txtUser1.Text = INIConfig.DBUsername;
                txtPassWord1.Text = INIConfig.DBPassword;

                //txtIP2.Text = INIConfig.ConfigDBSource;
                //nudPort2.Value = Convert.ToInt32(INIConfig.ConfigDBSourcePort);
                //txtDBName2.Text = INIConfig.ConfigDBName;
                //cbxSet.Items.Add("AlarmConfig");
                //cbxSet.Items.Add("EssConfig");
                //cbxSet.Items.Add("StationConfig");
                //cbxSet.Items.Add("StationStatus");
                //cbxSet.Items.Add("StationType");
                //cbxSet.SelectedIndex = 0;
                //txtUser2.Text = INIConfig.ConfigDBUsername;
                //txtPassWord2.Text = INIConfig.ConfigDBPassword;

                txtIP2.Text = INIConfig.DBSource;
                nudPort2.Value = Convert.ToInt32(INIConfig.DBSourcePort);
                txtDBName2.Text = INIConfig.ConfigDBName;
                cbxSet.Items.Add("AlarmConfig");
                cbxSet.Items.Add("EssConfig");
                cbxSet.Items.Add("StationConfig");
                cbxSet.Items.Add("StationStatus");
                cbxSet.Items.Add("StationType");
                cbxSet.SelectedIndex = 0;
                txtUser2.Text = INIConfig.DBUsername;
                txtPassWord2.Text = INIConfig.DBPassword;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog(ex, "加载数据库配置错误!");
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIP1.Text.Trim() == string.Empty || txtIP2.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入主机或IP地址!");
                    return;
                }

                if (txtDBName1.Text.Trim() == string.Empty || txtDBName2.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入数据库名!");
                    return;
                }

                INIFile.WriteObject("database", "DBSource", txtIP1.Text.Trim());
                INIFile.WriteObject("database", "DBSourcePort", nudPort1.Value == 0?"":nudPort1.Value.ToString());
                INIFile.WriteObject("database", "DBName", txtDBName1.Text.Trim());
                INIFile.WriteObject("database", "DBUsername", txtUser1.Text.Trim());
                INIFile.WriteObject("database", "DBPassword", txtPassWord1.Text.Trim());

                INIFile.WriteObject("database", "ConfigDBSource", txtIP2.Text.Trim());
                INIFile.WriteObject("database", "ConfigDBSourcePort", nudPort2.Value == 0 ? "" : nudPort2.Value.ToString());
                INIFile.WriteObject("database", "ConfigDBName", txtDBName2.Text.Trim());
                INIFile.WriteObject("database", "ConfigDBUsername", txtUser2.Text.Trim());
                INIFile.WriteObject("database", "ConfigDBPassword", txtPassWord2.Text.Trim());
                INIConfig.LoadSettings();
                Messages.ShowMessage("保存数据库配置成功，请重启采集程序!");
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog(ex, "保存数据库配置错误!");
            }
        }

        /// <summary>
        /// 测试实时数据表
        /// </summary>
        private void btnRealDataTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIP1.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入主机或IP地址!");
                    return;
                }

                if (txtDBName1.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入数据库名!");
                    return;
                }

                if (txtRealData.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入实时数据表!");
                    return;
                }

                DataBusiness.CheckDataTable(txtIP1.Text.Trim(), nudPort1.Value == 0 ? "" : nudPort1.Value.ToString(), INIConfig.RealDataDBName, txtUser1.Text.Trim(), txtPassWord1.Text.Trim(), txtRealData.Text.Trim());
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog(ex, "测试实时数据表失败!");
            }
        }

        /// <summary>
        /// 测试历史数据表
        /// </summary>
        private void btnHisTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIP1.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入主机或IP地址!");
                    return;
                }

                if (txtDBName1.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入数据库名!");
                    return;
                }

                if (txtHis.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入历史数据表!");
                    return;
                }

                DataBusiness.CheckDataTable(txtIP1.Text.Trim(), nudPort1.Value == 0 ? "" : nudPort1.Value.ToString(), txtDBName1.Text.Trim() + DateTime.Now.Year, txtUser1.Text.Trim(), txtPassWord1.Text.Trim(), txtHis.Text.Trim());
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog(ex, "测试历史数据表失败!");
            }
        }

        /// <summary>
        /// 测试配置表
        /// </summary>
        private void btnSettingTest_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIP2.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入主机或IP地址!");
                    return;
                }

                if (txtDBName2.Text.Trim() == string.Empty)
                {
                    Messages.ShowMessage("请输入数据库名!");
                    return;
                }

                DataBusiness.CheckDataTable(txtIP2.Text.Trim(), nudPort2.Value == 0 ? "" : nudPort2.Value.ToString(), txtDBName2.Text.Trim(), txtUser2.Text.Trim(), txtPassWord2.Text.Trim(), cbxSet.SelectedItem.ToString());
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog(ex, "测试配置数据表失败!");
            }
        }
    }
}
